//
//  SPLoginViewController.m
//  Sport
//
//  Created by Cass on 2018/3/23.
//  Copyright © 2018年 Cass. All rights reserved.
//

#import "SPLoginViewController.h"
#import "SPRegisterViewController.h"
#import "SPTabBarController.h"
#import "SPUserInfo.h"
@interface SPLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneText;
@property (weak, nonatomic) IBOutlet UITextField *passwordText;
@property (weak, nonatomic) IBOutlet UIImageView *phoneImageView;
@property (weak, nonatomic) IBOutlet UIImageView *passwordImageVIew;
@property (nonatomic, strong) NSMutableArray *userInfoArray;
@property (nonatomic, strong) SPUserInfo *userInfo;
@end

@implementation SPLoginViewController

//http://39.108.132.194/Sport/login.php
//accountName——账号
//password——密码

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
//    //读取上次的配置
//    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
//    //保存账号
//    self.phoneText.text = [userDefault objectForKey:@"phoneNumber"];
//    //保存密码
//    self.passwordText.text = [userDefault objectForKey:@"password"];
//
//    if ([self.phoneText.text isEqualToString:[userDefault objectForKey:@"phoneNumber"]] && [self.passwordText.text isEqualToString:[userDefault objectForKey:@"password"]]) {
//        [self login];
//    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.passwordText setSecureTextEntry:YES];
    UIVisualEffectView *extraLightView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
    extraLightView.frame = CGRectMake(0, 0, 140, 30);
    UIVisualEffectView *extraLightView1 = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
    extraLightView1.frame = CGRectMake(0, 0, 140, 30);
    [self.phoneImageView addSubview:extraLightView];
    [self.passwordImageVIew addSubview:extraLightView1];
}
//@"text/html",@"application/json", @"text/json", @"text/javascript", @"text/plain"
- (IBAction)loginBtnClick:(id)sender {
    [self login];
   
}

- (void)login{
    [[EMClient sharedClient] loginWithUsername:self.phoneText.text password:self.passwordText.text completion:^(NSString *aUsername, EMError *aError) {
        if (!aError) {
            NSLog(@"%@登录成功",aUsername);
        }else{
            NSLog(@"登录失败");
        }
    }];
    
    NSString *url = [NSString stringWithFormat:@"http://39.108.132.194/Sport/login.php"];
    NSDictionary *dict = @{
                           @"accountName":self.phoneText.text,
                           @"password":self.passwordText.text
                           };
    [SPHttpClient POST:url parameters:dict success:^(id responseObject) {
        NSLog(@"--账号信息--%@",responseObject);
        NSArray *info = responseObject;
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary *dict in info) {
            SPUserInfo *userInfo = [SPUserInfo mj_objectWithKeyValues:dict];
            [arr addObject:userInfo];
        }
        self.userInfoArray = arr;
        self.userInfo = [_userInfoArray objectAtIndex:0];
        [SVProgressHUD showWithStatus:@"登录中"];
        [SVProgressHUD dismissWithDelay:0.5];
        
        [self setUserDefaultInfo];
        //延时处理
        dispatch_time_t timer = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC));
        dispatch_after(timer, dispatch_get_main_queue(), ^{
            [self showTabbarVC];
        });
//        [self performSelector:@selector(showTabbarVC) withObject:nil afterDelay:0.5];
    } failure:^(NSError *error) {
        NSLog(@"--上传失败--%@",error);
        NSData *data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
        NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"服务器错误原因--%@",str);
        [SPHUDUtil showHUDWithDismissTimeInterval:0.5 message:@"账号或密码错误！" hudType:SPHUDTypeError];

    }];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)showTabbarVC{
    SPTabBarController *tabbarVC = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([SPTabBarController class])];
    [self presentViewController:tabbarVC animated:YES completion:nil];
}
#pragma mark - UserDefaults -
- (void)setUserDefaultInfo{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setBool:YES forKey:@"isLogin"];
    [userDefault setObject:self.phoneText.text forKey:@"phoneNumber"];
    [userDefault setObject:self.passwordText.text forKey:@"password"];
    [userDefault setObject:self.userInfo.nickname forKey:@"nickname"];
    [userDefault setObject:self.userInfo.ID forKey:@"ID"];
    [userDefault setObject:self.userInfo.age forKey:@"age"];
    [userDefault setObject:self.userInfo.accountName forKey:@"accountName"];
    [userDefault setObject:self.userInfo.gender forKey:@"gender"];
    [userDefault setObject:self.userInfo.hobby forKey:@"hobby"];
    [userDefault setObject:self.userInfo.headimage forKey:@"headimage"];
    [userDefault setObject:self.userInfo.occupation forKey:@"occupation"];
    [userDefault setObject:self.userInfo.persignature forKey:@"persignature"];
    [userDefault setObject:self.userInfo.location forKey:@"location"];
    
    [userDefault synchronize];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
